package com.muyu.vehicle.process;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.domain.VehicleFence;
import com.muyu.vehicle.domain.vo.VehicleFenceVo;
import com.muyu.vehicle.service.IVehicleFaultInfoService;
import com.muyu.vehicle.service.IVehicleFenceService;
import com.muyu.vehicle.service.IVehicleInfoService;
import com.vehicle.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author 牧鱼
 * @Classname ProcessVehicleOnLineThread
 * @Description TODO
 * @Date 2021/9/15
 */
public class SynchronizeFenceInfoThread implements Runnable{

    private static final Logger log = LoggerFactory.getLogger(SynchronizeFenceInfoThread.class);

    /**
     * redis
     */
    private final RedisTemplate<String,String> redisTemplate;

    /**
     * 围栏业务
     */
    private final IVehicleFenceService vehicleFenceService;
    /**
     * 需同步的VIN
     */
    private final String id;

    public SynchronizeFenceInfoThread(RedisTemplate<String, String> redisTemplate,
                                      IVehicleFenceService vehicleInfoService,
                                      String id) {
        this.redisTemplate = redisTemplate;
        this.vehicleFenceService = vehicleInfoService;
        this.id = id;
    }

    @Override
    public void run() {
        VehicleFence vehicleFenceSearch = new VehicleFence(){{
            setId(id);
        }};
        List<VehicleFence> vehicleFences = vehicleFenceService.selectVehicleFenceList(vehicleFenceSearch);
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        vehicleFences.forEach( vehicleFence -> {
            if (StringUtils.isNotEmpty(vehicleFence.getPath())){
                hashOperations.put(Config.VEHICLE_FENCE_INFO , vehicleFence.getId() , vehicleFence.getPath());
            }
            hashOperations.put(Config.VEHICLE_FENCE_TYPE , vehicleFence.getId() , vehicleFence.getFenceAlarmType());
            hashOperations.put(Config.VEHICLE_FENCE_TYPE , vehicleFence.getId()+"name" , vehicleFence.getFenceName());
        });
    }
}
